﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
//using POD.Common;
using System.Windows.Threading;
using System.Threading;
using System.Reflection;
using VMukti.Business.VMuktiGrid;
using System.Net;
using System.IO;
using VMuktiAPI;
using System.Collections;
using VMukti.Business.CommonDataContracts;
using VMukti.Presentation.Controls;

//imagename imgPODIcon

namespace VMuktiGrid.CustomGrid
{
    public partial class ctlPOD : UserControl
    {
        #region POD Variables

        private Point mouseOffsetForDrag;
        bool IsDraggingPOD = false;

        public int ObjectID = int.MinValue;
        public bool IsSaved = false;

        public Rectangle rectSuggetion = new Rectangle();

        private string strTitle = "";
        public string Title
        {
            get { return strTitle; }
            set
            {
                strTitle = value;
                lblTitle.Content = value;
            }
        }

        private int intColNo = -1;
        public int ColNo
        {
            get { return intColNo; }
            set { intColNo = value; }
        }

        bool IsThreadStarted = false;

        #endregion
        
        #region Module variables

        private int intModuleID = -1;
        public int ModuleID
        {
            get { return intModuleID; }
            set { intModuleID = value; }
        }

        private int _OwnerPodIndex = -1;
        public int OwnerPodIndex
        {
            get { return _OwnerPodIndex; }
            set { _OwnerPodIndex = value; }
        }

        bool _IsBuddyListVisible;
        public bool IsBuddyListVisible
        {
            get { return _IsBuddyListVisible; }
            set
            {
                if (value && _IsCollaborative)
                {
                    objBuddyList.Visibility = Visibility.Visible;
                }
                else if (!value)
                {
                    objBuddyList.Visibility = Visibility.Collapsed;
                }
                _IsBuddyListVisible = value;
            }
        }


        Assembly ass = null;
        Assembly a;
        public ArrayList al = new ArrayList();

        bool _IsCollaborative;
        int[] _arrPermissionValue;
        public int[] ModulePermissions
        {
            get { return _arrPermissionValue; }
        }

        bool _flag;

        string _strURI;
        public ItemsControl _objParent;
        string _strFromWhere;

        string[] _WCFUri;
        public string[] WCFUri
        {
            get
            {
                return _WCFUri;
            }
            set
            {
                _WCFUri = value;
            }
        }

        public delegate void delLoadModule(object objModuleParams);
        public delLoadModule objDelLoadModule = null;
        
        #endregion

        public ctlPOD(int intModID, string strModTitle, string strIsCollaborative, string strURI, int[] arrPermissionValue, bool flag, string strFromWhere, ItemsControl objParent)
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception exp)
            {
                //MessageBox.Show("CtlPod3 " + exp.Message);
                exp.Data.Add("My Key", "CtlPod--1--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
                
            }
            try
            {

                try
                {
                    FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "\\errlog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    StreamWriter sw = new StreamWriter(fs);
                    while (fs.ReadByte() != -1)
                    { }
                    sw.Write("Title: Error" + sw.NewLine);
                    sw.Write("Info Message: " + "I got URI as : " + strURI + sw.NewLine);
                    sw.Write("Date - Time: " + DateTime.Now.ToString() + sw.NewLine);
                    sw.Write("Location : ctlPOD()" + sw.NewLine);
                    sw.Write("===========================================================================================" + sw.NewLine);
                    sw.Close();
                    fs.Close();
                }
                catch
                {
                    //System.Windows.MessageBox.Show("Video--:--clsException.cs--:--WriteToErrorLogFile()--:--" + exp.Message);
                }

                _IsCollaborative = bool.Parse(strIsCollaborative);
                _strURI = strURI;
                _arrPermissionValue = arrPermissionValue;
                _flag = flag;
                _objParent = objParent;
                _strFromWhere = strFromWhere;

                objDelLoadModule = new delLoadModule(dispMethLoadModule);

                rectSuggetion.Fill = Brushes.Transparent;
                rectSuggetion.Stroke = Brushes.Red;

                DoubleCollection dblCol = new DoubleCollection();
                dblCol.Add(5.0);
                dblCol.Add(5.0);

                rectSuggetion.StrokeDashArray = dblCol;
                rectSuggetion.StrokeDashCap = PenLineCap.Round;
                rectSuggetion.StrokeDashOffset = 50;
                rectSuggetion.StrokeEndLineCap = PenLineCap.Square;
                rectSuggetion.StrokeLineJoin = PenLineJoin.Miter;
                rectSuggetion.StrokeMiterLimit = 50;
                rectSuggetion.RadiusX = 16;
                rectSuggetion.RadiusY = 16;
                rectSuggetion.Height = 100;

                Title = strModTitle;
                ModuleID = intModID;

                ass = Assembly.GetAssembly(typeof(ctlPOD));
                
                #region Events

                //this.Loaded += new RoutedEventHandler(ClsPod_Loaded);
                //this.Unloaded += new RoutedEventHandler(ClsPod_Unloaded);
                //this.PreviewMouseDown += new MouseButtonEventHandler(ClsPod_PreviewMouseDown);
                //this.cnvTitle.MouseDown += new MouseButtonEventHandler(cnvTitle_MouseDown);
                //this.cnvTitle.MouseUp += new MouseButtonEventHandler(cnvTitle_MouseUp);
                //this.btnMaximized.Click += new RoutedEventHandler(btnMaximized_Click);
                //this.btnMinimized.Click += new RoutedEventHandler(btnMinimized_Click);
                //this.cnvPodBody.SizeChanged += new SizeChangedEventHandler(cnvPodBody_SizeChanged);
                //cnvMainPOD.PreviewMouseMove += new MouseEventHandler(cnvMainPOD_PreviewMouseMove);
                //this.PreviewDrop += new DragEventHandler(CtlPod_PreviewDrop);
                //cnvPodBody.PreviewDrop += new DragEventHandler(cnvPodBody_PreviewDrop);

                #endregion

                this.Loaded += new RoutedEventHandler(ctlPOD_Loaded);
                if (_IsCollaborative)
                {
                    this.Drop += new DragEventHandler(ctlPOD_Drop);
                }
                else
                {
                    objBuddyList.Height = 0.0;
                    objBuddyList.Width = 0.0;
                }
                if (_objParent != null)
                {
                    ((ItemsControl)_objParent).Items.Add(this);
                    this.ColNo = (int)objParent.Tag;
                    if (_strFromWhere == "fromLeftPane")
                    {
                        grdTitle_PreviewMouseDown(this, new MouseButtonEventArgs(InputManager.Current.PrimaryMouseDevice, 0, MouseButton.Left));
                        this.AddBuddy(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName);
                    }
                    else if (_strFromWhere == "fromDatabase")
                    {
                        this.AddBuddy(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName);
                    }
                    else if (_strFromWhere == "fromOtherPeer")
                    {
                    }
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("CtlPod3 " + exp.Message);
                exp.Data.Add("My Key", "CtlPod--2--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
            }
        }

        void ctlPOD_Drop(object sender, DragEventArgs e)
        {
            //e.Handled = true;
            //this.AddBuddy(((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption);
            try
            {
                e.Handled = true;
                bool blnBuddyType = true;

                if (e.Data.GetDataPresent(typeof(CtlExpanderItem)))
                {
                    #region Check whether it is module or buddy dropped

                    string[] strTag = ((CtlExpanderItem)e.Data.GetData(typeof(CtlExpanderItem))).Tag.ToString().Split(',');
                    List<string> lstTag = new List<string>();
                    for (int i = 0; i < strTag.Length; i++)
                    {
                        if (strTag[i] == "ModuleType")
                        {
                            blnBuddyType = false;
                            break;
                        }
                    }

                    #endregion

                    if (blnBuddyType && this.AddBuddy(((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption))
                    {
                        VMuktiGrid.ctlPage.TabItem objSelectedPage = ((VMuktiGrid.ctlPage.TabItem)((VMuktiGrid.ctlTab.TabControl)((VMuktiGrid.ctlTab.TabItem)((ctlGrid)((Grid)((ItemsControl)this.Parent).Parent).Parent).Parent).Parent).Parent);
                        VMuktiGrid.ctlTab.TabItem objSelectedTab = ((VMuktiGrid.ctlTab.TabItem)((ctlGrid)((Grid)((ItemsControl)this.Parent).Parent).Parent).Parent);

                        clsPageInfo objPageInfo = new clsPageInfo();
                        objPageInfo.intPageID = objSelectedPage.ObjectID;
                        objPageInfo.strPageTitle = ((VMuktiGrid.CustomMenu.ctlPgTabHeader)objSelectedPage.Header).Title;

                        objPageInfo.intOwnerID = VMuktiAPI.VMuktiInfo.CurrentPeer.ID;
                        objPageInfo.intOwnerPageIndex = ((VMuktiGrid.ctlPage.TabControl)objSelectedPage.Parent).SelectedIndex;

                        objPageInfo.strDropType = "OnPod";

                        List<clsTabInfo> lstTabInfos = new List<clsTabInfo>();
                        lstTabInfos.Add(new clsTabInfo());

                        lstTabInfos[lstTabInfos.Count - 1].intTabID = objSelectedTab.ObjectID;
                        lstTabInfos[lstTabInfos.Count - 1].strTabTitle = ((VMuktiGrid.CustomMenu.ctlPgTabHeader)objSelectedTab.Header).Title;

                        List<string> lstBuddyList = new List<string>();

                        lstTabInfos[lstTabInfos.Count - 1].intOwnerTabIndex = ((VMuktiGrid.ctlTab.TabControl)objSelectedTab.Parent).SelectedIndex;
                        lstTabInfos[lstTabInfos.Count - 1].dblC1Width = ((VMuktiGrid.CustomGrid.ctlGrid)objSelectedTab.Content).LeftPanelContainer.ActualWidth;
                        lstTabInfos[lstTabInfos.Count - 1].dblC2Width = ((VMuktiGrid.CustomGrid.ctlGrid)objSelectedTab.Content).CentralPanelContainer.ActualWidth;
                        lstTabInfos[lstTabInfos.Count - 1].dblC3Width = ((VMuktiGrid.CustomGrid.ctlGrid)objSelectedTab.Content).RightPanelContainer.ActualWidth;

                        List<clsPodInfo> lstPodInfo = new List<clsPodInfo>();
                        VMuktiGrid.CustomGrid.ctlGrid objSelectedGrid = (VMuktiGrid.CustomGrid.ctlGrid)objSelectedTab.Content;

                        lstPodInfo.Add(new clsPodInfo());

                        lstPodInfo[lstPodInfo.Count - 1].intModuleId = this.ModuleID;
                        lstPodInfo[lstPodInfo.Count - 1].strPodTitle = this.Title;
                        lstPodInfo[lstPodInfo.Count - 1].strUri = this.WCFUri;
                        lstPodInfo[lstPodInfo.Count - 1].intColumnNumber = this.ColNo;
                        lstPodInfo[lstPodInfo.Count - 1].intOwnerPodIndex = this.OwnerPodIndex;

                        lstBuddyList.Clear();
                        StackPanel stPodBuddyList = this.objBuddyList.stPanel;
                        for (int i = 0; i < stPodBuddyList.Children.Count; i++)
                        {
                            if (stPodBuddyList.Children[i].GetType() == typeof(VMuktiGrid.Buddy.ctlBuddy))
                            {
                                lstBuddyList.Add(((VMuktiGrid.Buddy.ctlBuddy)stPodBuddyList.Children[i]).Title);
                            }
                        }
                        lstPodInfo[lstPodInfo.Count - 1].straPodBuddies = lstBuddyList.ToArray();

                        lstTabInfos[lstTabInfos.Count - 1].objaPods = lstPodInfo.ToArray();
                        objPageInfo.objaTabs = lstTabInfos.ToArray();

                        if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithNetP2P || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.BootStrap || VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.SuperNode)
                        {
                            VMukti.App.chNetP2PSuperNodeChannel.svcSetSpecialMsg(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption, "OPEN_PAGE", objPageInfo, VMuktiAPI.VMuktiInfo.CurrentPeer.SuperNodeIP);
                        }
                        else
                        {
                            try
                            {
                                VMukti.App.chHttpSuperNodeService.svcSetSpecialMsgs(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption, "OPEN_PAGE", objPageInfo);
                            }
                            catch (System.ServiceModel.EndpointNotFoundException ex)
                            {
                                ex.Data.Add("My Key", "ctlPOD_Drop()--:--CtlPOD.xaml.cs--:--" + ex.Message + " :--:--");
                                ClsException.LogError(ex);
                                ClsException.WriteToErrorLogFile(ex);
                                VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                                VMukti.App.chHttpSuperNodeService.svcSetSpecialMsgs(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption, "OPEN_PAGE", objPageInfo);
                            }
                            catch (System.ServiceModel.CommunicationException ex)
                            {
                                ex.Data.Add("My Key", "ctlPOD_Drop()--:--CtlPOD.xaml.cs--:--" + ex.Message + " :--:--");
                                ClsException.LogError(ex);
                                ClsException.WriteToErrorLogFile(ex);
                                VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                                VMukti.App.chHttpSuperNodeService.svcSetSpecialMsgs(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption, "OPEN_PAGE", objPageInfo);
                            }
                        }
                        //((ctlVMuktiGrid)((Grid)((VMuktiGrid.ctlPage.TabControl)((VMuktiGrid.ctlPage.TabItem)this.Parent).Parent).Parent).Parent).fncChannelSetPageMsg(VMuktiAPI.VMuktiInfo.CurrentPeer.DisplayName, ((VMukti.Presentation.Controls.CtlExpanderItem)e.Data.GetData(typeof(VMukti.Presentation.Controls.CtlExpanderItem))).Caption, "OPEN_PAGE", lstModuleInfo);
                        //VMukti.App.chNetP2PSuperNodeChannel.svcAddDraggedBuddy
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "ctlPOD_Drop()--:--ctlPOD.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        void ctlPOD_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (_objParent == null)
                {
                    if (this.Parent != null && this.Parent.GetType() == typeof(ItemsControl))
                    {
                        this.ColNo = (int)((ItemsControl)this.Parent).Tag;
                    }
                }
                if (!IsThreadStarted)
                {
                    IsThreadStarted = true;
                    LoadModule();
                }
                this.Loaded -= new RoutedEventHandler(ctlPOD_Loaded);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "ctlPOD_Loaded()--:--ctlPOD.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        private void LoadModule()
        {
            try
            {
                string[] tempUris = null;
                if (_IsCollaborative && _strURI == null)
                {
                    Thread thLoadModule = new Thread(new ParameterizedThreadStart(thMethLoadModule));
                    object[] objModuleParams = new object[3];
                    //objarray[0] = intModuleId;
                    //objarray[1] = strModuleTitle;
                    //string[] tempUris = ClsGlobal.clientHttpChannelSN.svcStartAService(VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType, intModuleId.ToString());
                    try
                    {
                        tempUris = VMukti.App.chHttpSuperNodeService.svcStartAService(VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType, intModuleID.ToString());
                    }
                    catch (System.ServiceModel.EndpointNotFoundException ex)
                    {
                        ex.Data.Add("My Key", "LoadModule()--:--CtlPOD.xaml.cs--:--" + ex.Message + " :--:--");
                        ClsException.LogError(ex);
                        ClsException.WriteToErrorLogFile(ex);
                        VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                        tempUris = VMukti.App.chHttpSuperNodeService.svcStartAService(VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType, intModuleID.ToString());
                    }
                    catch (System.ServiceModel.CommunicationException ex)
                    {
                        ex.Data.Add("My Key", "LoadModule()--:--CtlPOD.xaml.cs--:--" + ex.Message + " :--:--");
                        ClsException.LogError(ex);
                        ClsException.WriteToErrorLogFile(ex);
                        VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                        tempUris = VMukti.App.chHttpSuperNodeService.svcStartAService(VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType, intModuleID.ToString());
                    }

                    this.WCFUri = tempUris;
                    //if (VMuktiGlobal.objBindingType == BindingType.NetP2P)
                    if (VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.BootStrap || VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.SuperNode || VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithNetP2P)
                    {
                        objModuleParams[0] = tempUris[0];
                    }
                    //else if (VMuktiGlobal.objBindingType == BindingType.Http)
                    else if (VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithHttp)
                    {
                        objModuleParams[0] = tempUris[1];
                    }

                    objModuleParams[1] = _arrPermissionValue;
                    objModuleParams[2] = _flag;
                    thLoadModule.Start(objModuleParams);
                }
                else if (_IsCollaborative && _strURI != null)
                {
                    Thread thLoadModule = new Thread(new ParameterizedThreadStart(thMethLoadModule));
                    object[] objModuleParams = new object[3];
                    //Thread th = new Thread(new ParameterizedThreadStart(PageDispatcher));
                    //object[] objarray = new object[5];
                    //objarray[0] = intModuleId;
                    //objarray[1] = strModuleTitle;
                    objModuleParams[0] = _strURI;
                    objModuleParams[1] = _arrPermissionValue;
                    objModuleParams[2] = _flag;
                    //if (VMuktiGlobal.objBindingType == BindingType.NetP2P)
                    if (VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.BootStrap || VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.SuperNode || VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithNetP2P)
                    {
                        this.WCFUri = new string[2];
                        this.WCFUri[0] = _strURI;
                    }
                    //else if (VMuktiGlobal.objBindingType == BindingType.Http)
                    else if (VMuktiInfo.CurrentPeer.CurrPeerType == PeerType.NodeWithHttp)
                    {
                        this.WCFUri = new string[2];
                        this.WCFUri[1] = _strURI;
                    }
                    thLoadModule.Start(objModuleParams);
                }
                else
                {
                    this.WCFUri = null;

                    Thread thLoadModule = new Thread(new ParameterizedThreadStart(thMethLoadModule));
                    object[] objModuleParams = new object[2];
                    objModuleParams[0] = _arrPermissionValue;
                    objModuleParams[1] = _flag;
                    thLoadModule.Start(objModuleParams);
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "LoadModule()--:--ctlPOD.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        #region Module events

        public void thMethLoadModule(object objModuleParams)
        {
            try
            {
                this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, objDelLoadModule, objModuleParams);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "PageDispatcher()--:--CtlPod.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        public void dispMethLoadModule(object objModuleParams)
        { 
            try
            {
                object[] obj = (object[])objModuleParams;

                #region Collaborative

                if (obj.Length == 3)
                {
                    VMukti.Business.VMuktiGrid.ClsModule objModule = ClsModule.GetPodModule(this.ModuleID);

                    #region Downloading ZipFile

                    Uri zipFileURL = new Uri(VMuktiAPI.VMuktiInfo.ZipFileDownloadLink + objModule.ZipFile);
                    if (!Directory.Exists(ass.Location.Replace("VMukti.Presentation.exe", @"Zip Files")))
                    {
                        Directory.CreateDirectory(ass.Location.Replace("VMukti.Presentation.exe", @"Zip Files"));
                    }
                    string destination = ass.Location.Replace("VMukti.Presentation.exe", @"Zip Files");
                    if (!File.Exists(destination + "\\" + objModule.ZipFile))
                    {
                        WebClient wc = new WebClient();
                        wc.DownloadFile(zipFileURL, destination + "\\" + objModule.ZipFile);
                    }

                    #endregion

                    #region Extracting

                    string strModPath = ass.Location.Replace("VMukti.Presentation.exe", @"Modules");
                    VMukti.ZipUnzip.Zip.FastZip fz = new VMukti.ZipUnzip.Zip.FastZip();
                    if (!Directory.Exists(strModPath + "\\" + objModule.ZipFile.Split('.')[0]))
                    {
                        fz.ExtractZip(destination + "\\" + objModule.ZipFile, strModPath, null);
                    }

                    #endregion

                    #region Loading ReferencedAssemblies

                    //DirectoryInfo dirinfomodule = new DirectoryInfo(strModPath);
                    DirectoryInfo dirinfomodule = new DirectoryInfo(strModPath + "\\" + objModule.ZipFile.Split('.')[0]);
                    ShowDirectory(dirinfomodule);


                    #region extracting imagefile
                    string imagefilename = strModPath + "\\" + objModule.ZipFile.Split('.')[0] + "\\" + "Control\\" + objModule.ZipFile.Split('.')[0] + ".jpg";
                    if (File.Exists(imagefilename))
                    {
                        BitmapImage objimage = new BitmapImage();
                        objimage.BeginInit();
                        objimage.UriSource = new Uri(imagefilename);
                        objimage.EndInit();
                        imgPODIcon.BeginInit();
                        imgPODIcon.Source = objimage;
                        imgPODIcon.EndInit();
                    }
                    #endregion

                    for (int j = 0; j < al.Count; j++)
                    {
                        string[] arraysplit = al[j].ToString().Split('\\');
                        if (arraysplit[arraysplit.Length - 1].ToString() == objModule.AssemblyFile)
                        {
                            a = Assembly.LoadFrom(al[j].ToString());
                            AssemblyName[] an = a.GetReferencedAssemblies();

                            for (int alcount = 0; alcount < al.Count; alcount++)
                            {
                                string strsplit = al[alcount].ToString();
                                string[] strold = strsplit.Split('\\');
                                string strnew = strold[strold.Length - 1].Substring(0, strold[strold.Length - 1].Length - 4);

                                for (int asscount = 0; asscount < an.Length; asscount++)
                                {
                                    if (an[asscount].Name == strnew)
                                    {
                                        Assembly assbal = System.AppDomain.CurrentDomain.Load(System.Reflection.Assembly.LoadFrom(al[alcount].ToString()).GetName());
                                        AssemblyName[] anbal = assbal.GetReferencedAssemblies();
                                        for (int andal = 0; andal < al.Count; andal++)
                                        {
                                            string strsplitdal = al[andal].ToString();
                                            string[] strolddal = strsplitdal.Split('\\');
                                            string strnewdal = strolddal[strolddal.Length - 1].Substring(0, strolddal[strolddal.Length - 1].Length - 4);

                                            for (int asscountdal = 0; asscountdal < anbal.Length; asscountdal++)
                                            {
                                                if (anbal[asscountdal].Name == strnewdal)
                                                {
                                                    Assembly assdal = System.AppDomain.CurrentDomain.Load(System.Reflection.Assembly.LoadFrom(al[andal].ToString()).GetName());
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            Type[] t1 = a.GetTypes();

                            #region CreatingObject

                            for (int k = 0; k < t1.Length; k++)
                            {
                                if (t1[k].Name == objModule.ClassName)
                                {
                                    try
                                    {
                                        ConstructorInfo[] ci = t1[k].GetConstructors();

                                        for (int constcount = 0; constcount < ci.Length; constcount++)
                                        {
                                            ParameterInfo[] pi = ci[constcount].GetParameters();
                                            if (pi.Length == 4)
                                            {
                                                if (pi[0].ParameterType.Name == "PeerType")
                                                {
                                                    object[] objArg = new object[4];
                                                    //objArg[0] = VMuktiGlobal.objBindingType;
                                                    //VMuktiInfo.CurrentPeer.CurrPeerType = VMuktiAPI.PeerType.NodeWithNetP2P;
                                                    objArg[0] = VMuktiInfo.CurrentPeer.CurrPeerType;
                                                    objArg[1] = obj[0].ToString();
                                                    objArg[2] = obj[1];
                                                    if (_strFromWhere == "fromOtherPeer")
                                                    {
                                                        objArg[3] = "Guest";
                                                    }
                                                    else if (_strFromWhere == "fromLeftPane" || _strFromWhere == "fromDatabase")
                                                    {
                                                        objArg[3] = "Host";
                                                    }

                                                    object obj1 = Activator.CreateInstance(t1[k], BindingFlags.CreateInstance, null, objArg, new System.Globalization.CultureInfo("en-US"));
                                                    //((UserControl)obj1).SetValue(Canvas.LeftProperty, 0.0);
                                                    //((UserControl)obj1).SetValue(Canvas.TopProperty, 0.0);

                                                    grdBody.Tag = t1[k].ToString();

                                                    if (!(bool)obj[2])
                                                    {
                                                        if (((UserControl)obj1).MinHeight.ToString() != "0")
                                                        {
                                                            //this.Height = ((UserControl)obj1).MinHeight + 20.0;
                                                            //this.Width = ((UserControl)obj1).MinWidth + 13.0;

                                                            //((Resizer)(this.Parent)).Height = this.Height + 5.0;

                                                            //((Resizer)(this.Parent)).Width = this.Width + 5.0;
                                                        }
                                                        else
                                                        {
                                                            //if (cnvPodBody.Height - 3.0 >= 0)
                                                            //{
                                                            //    ((UserControl)obj1).Height = cnvPodBody.Height - 3.0;
                                                            //}
                                                            //else
                                                            //{
                                                            //    ((UserControl)obj1).Height = 0.0;
                                                            //}
                                                            //if (cnvPodBody.Width - 3.0 >= 0)
                                                            //{
                                                            //    ((UserControl)obj1).Width = (cnvPodBody.Width - 3.0);
                                                            //}
                                                            //else
                                                            //{
                                                            //    ((UserControl)obj1).Width = 0.0;
                                                            //}

                                                        }

                                                    }

                                                    //if (cnvPodBody.Height - 3.0 >= 0)
                                                    //{
                                                    //    ((UserControl)obj1).Height = cnvPodBody.Height - 3.0;
                                                    //}
                                                    //else
                                                    //{
                                                    //    ((UserControl)obj1).Height = 0.0;
                                                    //}
                                                    //if (cnvPodBody.Width - 3.0 >= 0)
                                                    //{
                                                    //    ((UserControl)obj1).Width = (cnvPodBody.Width - 3.0);
                                                    //}
                                                    //else
                                                    //{
                                                    //    ((UserControl)obj1).Width = 0.0;
                                                    //}
                                                    //((UserControl)obj1).Drop += new DragEventHandler(CtlPod_Drop);                                                 
                                                    //setdisplayname();
                                                    grdBody.Children.Remove(animImage);
                                                    grdBody.Children.Add((UIElement)obj1);
                                                    //cnvPodBody.Children.Add((UIElement)obj1);
                                                    break;
                                                }
                                            }
                                            else
                                            {
                                                object obj1 = Activator.CreateInstance(t1[k]);
                                                //object obj1 = Activator.CreateInstance(t1[k], BindingFlags.CreateInstance, new Binder(), objArg, new System.Globalization.CultureInfo("en-US"));
                                                ((UserControl)obj1).SetValue(Canvas.LeftProperty, 0.0);
                                                ((UserControl)obj1).SetValue(Canvas.TopProperty, 0.0);

                                                grdBody.Tag = t1[k].ToString();
                                                if (((UserControl)obj1).MinHeight.ToString() != "0")
                                                {
                                                    //this.Height = ((UserControl)obj1).MinHeight + 20.0;
                                                    //this.Width = ((UserControl)obj1).MinWidth + 13.0;

                                                    //((Resizer)(this.Parent)).Height = this.Height + 5.0;

                                                    //((Resizer)(this.Parent)).Width = this.Width + 5.0;
                                                }
                                                else
                                                {
                                                    //if (cnvPodBody.Height - 3.0 >= 0)
                                                    //{
                                                    //    ((UserControl)obj1).Height = cnvPodBody.Height - 3.0;
                                                    //}
                                                    //else
                                                    //{
                                                    //    ((UserControl)obj1).Height = 0.0;
                                                    //}
                                                    //if (cnvPodBody.Width - 3.0 >= 0)
                                                    //{
                                                    //    ((UserControl)obj1).Width = (cnvPodBody.Width - 3.0);
                                                    //}
                                                    //else
                                                    //{
                                                    //    ((UserControl)obj1).Width = 0.0;
                                                    //}

                                                }
                                                //if (cnvPodBody.Height - 3.0 >= 0)
                                                //{
                                                //    ((UserControl)obj1).Height = cnvPodBody.Height - 3.0;
                                                //}
                                                //else
                                                //{
                                                //    ((UserControl)obj1).Height = 0.0;
                                                //}
                                                //if (cnvPodBody.Width - 3.0 >= 0)
                                                //{
                                                //    ((UserControl)obj1).Width = (cnvPodBody.Width - 3.0);
                                                //}
                                                //else
                                                //{
                                                //    ((UserControl)obj1).Width = 0.0;
                                                //}

                                                grdBody.Children.Remove(animImage);
                                                grdBody.Children.Add((UIElement)obj1);
                                                //cnvPodBody.Children.Add((UIElement)obj1);
                                                break;
                                                
                                            }
                                        }
                                    }
                                    catch (Exception exp)
                                    {
                                        //MessageBox.Show("CreatingObject " + exp.Message);
                                        exp.Data.Add("My Key", "CreatingObject-5--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                                        ClsException.LogError(exp);
                                        ClsException.WriteToErrorLogFile(exp);
                                    }
                                    //lbPodBody.Visibility = Visibility.Hidden;
                                    //this.PodVisible(true);
                                }
                            }
                            #endregion
                        }
                    }
                    #endregion
                }
                #endregion

                #region NonCollaborative
                else
                {
                    VMukti.Business.VMuktiGrid.ClsModule objModule = ClsModule.GetPodModule(this.ModuleID);

                    #region Downloading ZipFile

                    Uri zipFileURL = new Uri(VMuktiAPI.VMuktiInfo.ZipFileDownloadLink + objModule.ZipFile);
                    if (!Directory.Exists(ass.Location.Replace("VMukti.Presentation.exe", @"Zip Files")))
                    {
                        Directory.CreateDirectory(ass.Location.Replace("VMukti.Presentation.exe", @"Zip Files"));
                    }
                    string destination = ass.Location.Replace("VMukti.Presentation.exe", @"Zip Files");
                    if (!File.Exists(destination + "\\" + objModule.ZipFile))
                    {
                        WebClient wc = new WebClient();
                        wc.DownloadFile(zipFileURL, destination + "\\" + objModule.ZipFile);
                    }

                    #endregion

                    #region Extracting

                    string strModPath = ass.Location.Replace("VMukti.Presentation.exe", @"Modules");
                    VMukti.ZipUnzip.Zip.FastZip fz = new VMukti.ZipUnzip.Zip.FastZip();
                  
                    if (!Directory.Exists(strModPath + "\\" + objModule.ZipFile.Split('.')[0]))
                    {
                        fz.ExtractZip(destination + "\\" + objModule.ZipFile, strModPath, null);
                    }

                    #endregion

                    #region Loading ReferencedAssemblies

                    //DirectoryInfo dirinfomodule = new DirectoryInfo(strModPath);
                    DirectoryInfo dirinfomodule = new DirectoryInfo(strModPath + "\\" + objModule.ZipFile.Split('.')[0]);
                    ShowDirectory(dirinfomodule);


                    #region extracting imagefile
                    string imagefilename = strModPath + "\\" + objModule.ZipFile.Split('.')[0] + "\\" + "Control\\" + objModule.ZipFile.Split('.')[0] + ".jpg";
                    if (File.Exists(imagefilename))
                    {
                        BitmapImage objimage = new BitmapImage();
                        objimage.BeginInit();
                        objimage.UriSource = new Uri(imagefilename);
                        objimage.EndInit();
                        imgPODIcon.BeginInit();
                        imgPODIcon.Source = objimage;
                        imgPODIcon.EndInit();
                    }
                    #endregion


                    for (int j = 0; j < al.Count; j++)
                    {
                        string[] arraysplit = al[j].ToString().Split('\\');
                        if (arraysplit[arraysplit.Length - 1].ToString() == objModule.AssemblyFile)
                        {
                            a = Assembly.LoadFrom(al[j].ToString());
                            AssemblyName[] an = a.GetReferencedAssemblies();

                            for (int alcount = 0; alcount < al.Count; alcount++)
                            {
                                string strsplit = al[alcount].ToString();
                                string[] strold = strsplit.Split('\\');
                                string strnew = strold[strold.Length - 1].Substring(0, strold[strold.Length - 1].Length - 4);

                                for (int asscount = 0; asscount < an.Length; asscount++)
                                {
                                    if (an[asscount].Name == strnew)
                                    {
                                        Assembly assbal = System.AppDomain.CurrentDomain.Load(System.Reflection.Assembly.LoadFrom(al[alcount].ToString()).GetName());
                                        AssemblyName[] anbal = assbal.GetReferencedAssemblies();
                                        for (int andal = 0; andal < al.Count; andal++)
                                        {
                                            string strsplitdal = al[andal].ToString();
                                            string[] strolddal = strsplitdal.Split('\\');
                                            string strnewdal = strolddal[strolddal.Length - 1].Substring(0, strolddal[strolddal.Length - 1].Length - 4);

                                            for (int asscountdal = 0; asscountdal < anbal.Length; asscountdal++)
                                            {
                                                if (anbal[asscountdal].Name == strnewdal)
                                                {
                                                    Assembly assdal = System.AppDomain.CurrentDomain.Load(System.Reflection.Assembly.LoadFrom(al[andal].ToString()).GetName());
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            Type[] t1 = a.GetTypes();

                            #region CreatingObject

                            for (int k = 0; k < t1.Length; k++)
                            {
                                if (t1[k].Name == objModule.ClassName)
                                {
                                    try
                                    {
                                        object[] objArg = new object[1];
                                        objArg[0] = obj[0];

                                        object obj1 = Activator.CreateInstance(t1[k], objArg);
                                        //object obj1 = Activator.CreateInstance(t1[k], BindingFlags.Public,null,objArg, new System.Globalization.CultureInfo("en-US"));
                                        ((UserControl)obj1).SetValue(Canvas.LeftProperty, 0.0);
                                        ((UserControl)obj1).SetValue(Canvas.TopProperty, 0.0);

                                        grdBody.Tag = t1[k].ToString();
                                        //cnvPodBody.Tag = t1[k].ToString();

                                        if (!(bool)obj[1])
                                        {
                                            if (((UserControl)obj1).MinHeight.ToString() != "0")
                                            {
                                                //this.Height = ((UserControl)obj1).MinHeight + 20.0;
                                                //this.Width = ((UserControl)obj1).MinWidth + 13.0;

                                                //this.DBHeight = this.Height;
                                                //this.DBWidth = this.Width;

                                                //((Resizer)(this.Parent)).Height = this.Height + 5.0;

                                                //((Resizer)(this.Parent)).Width = this.Width + 5.0;

                                                //double top = double.Parse(this.GetValue(Canvas.TopProperty).ToString());
                                                //MessageBox.Show(this.GetValue(Canvas.LeftProperty).ToString());
                                                //this.SetValue(Canvas.TopProperty,
                                            }
                                            else
                                            {
                                                //if (cnvPodBody.Height - 3.0 >= 0)
                                                //{
                                                //    ((UserControl)obj1).Height = cnvPodBody.Height - 3.0;
                                                //}
                                                //else
                                                //{
                                                //    ((UserControl)obj1).Height = 0.0;
                                                //}
                                                //if (cnvPodBody.Width - 3.0 >= 0)
                                                //{
                                                //    ((UserControl)obj1).Width = (cnvPodBody.Width - 3.0);
                                                //}
                                                //else
                                                //{
                                                //    ((UserControl)obj1).Width = 0.0;
                                                //}
                                            }
                                        }

                                        grdBody.Children.Remove(animImage);
                                        grdBody.Children.Add((UIElement)obj1);
                                    }
                                    catch (Exception exp)
                                    {
                                        //MessageBox.Show("CreatingObject " + exp.Message);
                                        exp.Data.Add("My Key", "CreatingObject-7--:--CtlPageTab.cs--:--" + exp.Message + " :--:--");
                                        ClsException.LogError(exp);
                                        ClsException.WriteToErrorLogFile(exp);
                                        //cnvPodBody.Children.Add(new UIElement());
                                        grdBody.Children.Add(new UIElement());
                                    }
                                    //lbPodBody.Visibility = Visibility.Hidden;
                                    //this.PodVisible(true);
                                    //this.Visibility = Visibility.Visible;
                                }
                            }
                            #endregion
                        }
                    }
                    #endregion

                }
                #endregion

                al.Clear();
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "PageThread()--:--CtlPod.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        public void ShowDirectory(DirectoryInfo dir)
        {
            try
            {
                foreach (FileInfo file in dir.GetFiles("*.dll"))
                {
                    int hj = al.Add(file.FullName);
                }
                foreach (DirectoryInfo subDir in dir.GetDirectories())
                {
                    ShowDirectory(subDir);
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "ShowDirectory()--:--CtlPod.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        #endregion

        public bool AddBuddy(string strBuddyName)
        {
            try
            {
                return objBuddyList.AddBuddy(strBuddyName);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "AddBuddy()--:--ctlPOD.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return false;
            }
        }

        void ctlPOD_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (!this.IsDraggingPOD && VMuktiHelper.IsDraggingPOD && e.GetPosition(rowDef2 as IInputElement).Y > 25.0)
                {
                    rectSuggetion.Height = VMuktiHelper.RectSuggestHeight;
                    foreach (object UIItem in ((ItemsControl)this.Parent).Items)
                    {
                        if (UIItem.GetType() == typeof(Rectangle))
                        {
                            ((ItemsControl)this.Parent).Items.Remove(UIItem);
                            break;
                        }
                    }
                    ((ItemsControl)this.Parent).Items.Insert(((ItemsControl)this.Parent).Items.IndexOf(this) + 1, rectSuggetion);
                    this.MouseMove -= new MouseEventHandler(ctlPOD_MouseMove);
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "ctlPOD_MouseMove()--:--ctlPOD.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        void brdPOD_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                if (!this.IsDraggingPOD && VMuktiHelper.IsDraggingPOD)
                {
                    this.MouseMove += new MouseEventHandler(ctlPOD_MouseMove);

                    rectSuggetion.Height = VMuktiHelper.RectSuggestHeight;
                ab:
                    foreach (object UIItem in ((ItemsControl)this.Parent).Items)
                    {
                        if (UIItem.GetType() == typeof(Rectangle))
                        {
                            ((ItemsControl)this.Parent).Items.Remove(UIItem);
                            goto ab;
                        }
                    }
                    ((ItemsControl)this.Parent).Items.Insert(((ItemsControl)this.Parent).Items.IndexOf(this), rectSuggetion);
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "brdPOD_MouseEnter()--:--ctlPOD.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }
        
        #region grid events

        public void grdTitle_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                this.Opacity = 0.5;

                this.IsDraggingPOD = true;
                VMuktiHelper.IsDraggingPOD = true;
                this.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(Object state)
                {
                    VMuktiHelper.RectSuggestHeight = this.ActualHeight;
                    return null;
                }), null);

                rectSuggetion.Height = VMuktiHelper.RectSuggestHeight;
            ab:
                foreach (object UIItem in ((ItemsControl)this.Parent).Items)
                {
                    if (UIItem.GetType() == typeof(Rectangle))
                    {
                        ((ItemsControl)this.Parent).Items.Remove(UIItem);
                        goto ab;
                    }
                }
                ((ItemsControl)this.Parent).Items.Insert(((ItemsControl)this.Parent).Items.IndexOf(this), rectSuggetion);

                mouseOffsetForDrag = e.MouseDevice.GetPosition(this as IInputElement);

                Application.Current.MainWindow.MouseMove += new System.Windows.Input.MouseEventHandler(Parent_MouseMove);
                Application.Current.MainWindow.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(Parent_MouseLeftButtonUp);

                base.RaiseEvent(new RoutedEventArgs(ctlPOD.OnPODDragEvent, this));
                e.MouseDevice.Capture(this as IInputElement);
                e.MouseDevice.Capture(null);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "grdTitle_PreviewMouseDown()--:--ctlPOD.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        void Parent_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                this.Opacity = 1.0;

                this.MouseMove += new MouseEventHandler(ctlPOD_MouseMove);

                IsDraggingPOD = false;
                VMuktiHelper.IsDraggingPOD = false;

                this.RenderTransform = null;
                FrameworkElement header = sender as FrameworkElement;

                Application.Current.MainWindow.MouseMove -= Parent_MouseMove;
                Application.Current.MainWindow.MouseLeftButtonUp -= Parent_MouseLeftButtonUp;

                base.RaiseEvent(new RoutedEventArgs(ctlPOD.OnPODDropEvent, this));

                if (!IsThreadStarted)
                {
                    IsThreadStarted = true;
                    LoadModule();
                }

                if (this.Parent.GetType() == typeof(ItemsControl) && ((ctlGrid)((Grid)((ItemsControl)this.Parent).Parent).Parent).IsDragEnter == false)
                {
                    ((ctlGrid)((Grid)((ItemsControl)this.Parent).Parent).Parent).IsDragEnter = true;
                    ((ctlGrid)((Grid)((ItemsControl)this.Parent).Parent).Parent).DragEnter += new DragEventHandler(((ctlGrid)((Grid)((ItemsControl)this.Parent).Parent).Parent).ctlGrid_PreviewDragEnter);
                    //MessageBox.Show(this.Parent.GetType().ToString());
                }
                //e.MouseDevice.Capture(null);
                //((ItemsControl)this.Parent).Items.RemoveAt(((ItemsControl)this.Parent).Items.IndexOf(this) - 1);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "Parent_MouseLeftButtonUp()--:--ctlPOD.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        void Parent_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            try
            {
                e.MouseDevice.Synchronize();

                //Point mouseOffsetRelativeToParent = e.MouseDevice.GetPosition(Application.Current.MainWindow as IInputElement);
                Point mouseOffsetRelativeToParent = e.MouseDevice.GetPosition(this.Parent as IInputElement);
                Point offsetTansformDistance = new Point(mouseOffsetRelativeToParent.X - mouseOffsetForDrag.X, mouseOffsetRelativeToParent.Y - mouseOffsetForDrag.Y);

                this.RenderTransform = new TranslateTransform(offsetTansformDistance.X, offsetTansformDistance.Y);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "Parent_MouseMove()--:--ctlPOD.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        #endregion

        public event RoutedEventHandler OnPODDrop
        {
            add
            {
                base.AddHandler(ctlPOD.OnPODDropEvent, value);
            }
            remove
            {
                base.RemoveHandler(ctlPOD.OnPODDropEvent, value);
            }
        }

        public static readonly RoutedEvent OnPODDropEvent = EventManager.RegisterRoutedEvent("OnPanelDrop", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ctlPOD));

        public event RoutedEventHandler OnPODDrag
        {
            add
            {
                base.AddHandler(ctlPOD.OnPODDragEvent, value);
            }
            remove
            {
                base.RemoveHandler(ctlPOD.OnPODDragEvent, value);
            }
        }

        public static readonly RoutedEvent OnPODDragEvent = EventManager.RegisterRoutedEvent("OnPanelDrag", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ctlPOD));

        #region Pod title bar events

        private void btnMinPanel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if ((grdTitle.Visibility == Visibility.Visible) && ((RotateTransform)((TransformGroup)btnMinPanel.GetValue(Button.RenderTransformProperty)).Children[2]).Angle == 90)
                {
                    grdTitle.Visibility = Visibility.Collapsed;
                    ((RotateTransform)((TransformGroup)btnMinPanel.GetValue(Button.RenderTransformProperty)).Children[2]).Angle = 270;
                    btnHidePanel.Visibility = Visibility.Collapsed;
                    btnMinPanel.Visibility = Visibility.Collapsed;
                    rowDef1.Height = new GridLength(0, GridUnitType.Pixel);
                    btnMinPanel1.Margin = new Thickness(0, 5, 0, 0);
                    btnMinPanel1.Width = 16.0;
                    //LocalUserControl.Height = ucControl + 25.0;
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "btnMinPanel_Click()--:--ctlPOD.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ObjectID != int.MinValue)
                {
                    ((VMuktiGrid.CustomGrid.ctlGrid)((Grid)((ItemsControl)this.Parent).Parent).Parent).DeletePOD(ObjectID);
                }
                ((ItemsControl)this.Parent).Items.Remove(this);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "btnClose_Click()--:--ctlPOD.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
            //this.Visibility = Visibility.Collapsed;
        }

        private void btnHidePanel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if ((grdBody.Visibility == Visibility.Visible) && ((RotateTransform)((TransformGroup)btnHidePanel.GetValue(Button.RenderTransformProperty)).Children[2]).Angle == 0)
                {
                    grdBody.Visibility = Visibility.Collapsed;
                    ((RotateTransform)((TransformGroup)btnHidePanel.GetValue(Button.RenderTransformProperty)).Children[2]).Angle = 270;
                    btnMinPanel.Visibility = Visibility.Collapsed;
                    btnClose.Margin = new Thickness(0, 5, 5, 5);
                    btnMinPanel.Margin = new Thickness(0, -100, 0, 0);
                    brdPOD.CornerRadius = new CornerRadius(16, 16, 16, 16);
                }
                else
                {
                    grdBody.Visibility = Visibility.Visible;
                    ((RotateTransform)((TransformGroup)btnHidePanel.GetValue(Button.RenderTransformProperty)).Children[2]).Angle = 0;
                    btnMinPanel.Visibility = Visibility.Visible;
                    btnClose.Margin = new Thickness(0, 5, 17, 5);
                    btnMinPanel.Margin = new Thickness(0, 0, 0, 0);
                    brdPOD.CornerRadius = new CornerRadius(16, 16, 0, 0);
                }
                this.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(Object state)
                {
                    VMuktiHelper.RectSuggestHeight = this.ActualHeight;
                    return null;
                }), null);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "btnHidePanel_Click()--:--ctlPOD.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        private void btnMinPanel1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                grdTitle.Visibility = Visibility.Visible;
                ((RotateTransform)((TransformGroup)btnMinPanel.GetValue(Button.RenderTransformProperty)).Children[2]).Angle = 90;
                btnHidePanel.Visibility = Visibility.Visible;
                btnMinPanel.Visibility = Visibility.Visible;
                rowDef1.Height = new GridLength(25, GridUnitType.Pixel);
                btnMinPanel1.Visibility = Visibility.Collapsed;
                btnMinPanel1.Margin = new Thickness(0, -100, 0, 0);
                btnMinPanel1.Width = 0.0;
                //LocalUserControl.Height = ucControl;
                this.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(Object state)
                {
                    VMuktiHelper.RectSuggestHeight = this.ActualHeight;
                    return null;
                }), null);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "btnMinPanel1_Click()--:--ctlPOD.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        #endregion

        public void Save(int tabID)
        {
            try
            {
                if (!IsSaved)
                {
                    if (ObjectID == int.MinValue)
                    {
                        ObjectID = VMukti.Business.VMuktiGrid.ClsPod.AddPod(-1, tabID, strTitle, intColNo, "", ModuleID, VMuktiAPI.VMuktiInfo.CurrentPeer.ID);
                    }
                    else
                    {
                        VMukti.Business.VMuktiGrid.ClsPod.AddPod(ObjectID, tabID, strTitle, intColNo, "", ModuleID, VMuktiAPI.VMuktiInfo.CurrentPeer.ID);
                    }
                    IsSaved = true;
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "Save()--:--ctlPOD.xmal.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        public void Delete()
        {
            //if (ObjectID != int.MinValue)
            //{
            //    try
            //    {
            //        (new VMukti.Business.VMuktiGrid.ClsPod()).Remove_Pod(ObjectID);
            //    }
            //    catch (Exception exp)
            //    {
            //        MessageBox.Show(exp.Message);
            //    }
            //}
        }
    }
}
